<?php

namespace app\models;


use app\librarys\DbCacheUtility;

class CardShow
{
//    public static function getIntegralCardTPL()
//    {
//        $sql = 'SELECT * FROM DLB_INTEGRAL_CARD_TPL';
//        $cmd = \Yii::$app->db->createCommand($sql);
//
//        $items = $cmd->queryAll();
//        return $items;
//    }

    public static function getIntegralCard_IMG($id)
    {
        $sql = 'select page_image_url, name from t_card_tpl where id=:id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);
        $items = DbCacheUtility::queryAll($cmd);
        return $items;
    }

    public static function getIntegralCardTPLByID($card_template_id)
    {
        $sql = 'SELECT * FROM t_card_tpl WHERE ID =:Id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':Id', $card_template_id);
        $items = DbCacheUtility::queryOne($cmd);
        return $items;
    }

//    public static function getIntegralCardTPLGoodsCount($card_template_id)
//    {
//        $IntegralCardTPL = CardShow::getIntegralCardTPLByID($card_template_id);
//        $price = 0;
//        if ($IntegralCardTPL != null) {
//            $price = $IntegralCardTPL['integral'];
//        }
//
//        $sql = 'SELECT COUNT(1) FROM (
//                  SELECT t1.*,nvl(t2.SORT,-1) FROM DLB_PRODUCTS t1 LEFT JOIN (
//                              SELECT GOODS_CODE AS CODE,SORT FROM DLB_INTEGRAL_CARD_TPL_GOODS WHERE CARD_TEMPLATE_ID = :CARD_TEMPLATE_ID
//                              ) t2 ON t2.CODE = t1.CODE
//                            WHERE  (t2.CODE IS NOT NULL OR (t1.PRICE <= :PRICE AND t1.PRICE > 0)) AND t1.STATUS = 1
//
//                )';
//        $cmd = \Yii::$app->db->createCommand($sql);
//        $cmd->bindValue(':CARD_TEMPLATE_ID', $card_template_id);
//        $cmd->bindValue(':PRICE', $price);
//
//        $count = $cmd->queryScalar();
//        return $count;
//    }

    public static function getIntegralCardTPLGoods($startIndex, $pageSize, $card_template_id)
    {
        $IntegralCardTPL = CardShow::getIntegralCardTPLByID($card_template_id);
        $price = 0;
        if ($IntegralCardTPL != null) {
            $price = $IntegralCardTPL['integral'];
        }

        $sql = "select tt.*, t11.image_url from 
                         (select * from(
                        (select t1.*,0 as temp_type from t_card_tpl_goods t0
                        left join t_goods t1 on t1.code=t0.goods_code and t1.institution_code='0' and t1.is_enabled=1 and t1.is_shelved=1
                         where card_tpl_id=:card_template_id)
                        UNION
                        (
                        select *,1 as temp_type from t_goods where institution_code='0' and selling_price<:price and is_enabled=1 and is_shelved=1 and is_show=1 
                        ))  as t) as tt
                         join t_goods_image t11 on t11.goods_code=tt.code and t11.type=1
                        order by tt.temp_type asc,tt.selling_price desc,tt.is_crossborder desc limit :startIndex, :page_size";

        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':card_template_id', $card_template_id);
        $cmd->bindValue(':price', (int)$price);
        $cmd->bindValue(':startIndex', $startIndex);
        $cmd->bindValue(':page_size', $pageSize);
        $items = DbCacheUtility::queryAll($cmd);
        return $items;
    }

//    public static function getProduct($product_code)
//    {
//        $sql = 'SELECT * FROM DLB_PRODUCTS WHERE CODE = :code';
//        $cmd = \Yii::$app->db->createCommand($sql);
//        $cmd->bindValue(':code', $product_code);
//        $item = $cmd->queryOne();
//        return $item;
//    }
//
    public static function getGoodsDetail($goodsCode)
    {
        $sql = "SELECT detail_html FROM t_goods_detail_html WHERE `code`=:goodsCode AND institution_code='0'";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':goodsCode', $goodsCode);
        $item = DbCacheUtility::queryOne($cmd);
        if ($item == false) {
            $item = '';
        }
        return $item;
    }
}